package com.juli.school.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.Contact;
import com.juli.school.dao.mapper.ContactMapper;
import com.juli.school.service.bo.ContactBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;

/**
 * @Description: 联系方式服务类
 * @author: wjp
 * @date: 2018年1月29日下午9:18:04
 */
@Service
public class ContactService {

	private static final Logger logger = LoggerFactory.getLogger(ContactService.class);

	@Resource
	ContactMapper contactMapper;

	public CommonResp<List<ContactBo>> list(Contact contact) {
		CommonResp<List<ContactBo>> commonResp = new CommonResp<List<ContactBo>>();
		try {
			if (StringUtils.isBlank(contact.getType())) {
				commonResp.fail("入参必须包含type");
				return commonResp;
			}
			if(StringUtils.isBlank(contact.getStatus())){
				contact.setStatus("启用");
			}
			List<Contact> listPage = contactMapper.list(contact);
			List<ContactBo> data = Lists.newArrayList();
			for (Contact findContact : listPage) {
				ContactBo cb = new ContactBo();
				BeanUtils.copyProperties(cb, findContact);
				data.add(cb);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (BeansException e) {
			logger.error("获取联系人发生异常", e);
			commonResp.exception("获取联系人发生异常");
		}
		return commonResp;
	}

	public CommonResp<String> add(Contact contact) {
		CommonResp<String> commonResp = new CommonResp<String>();
		try {
			contactMapper.insert(contact);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增联系人信息异常", e);
			commonResp.exception("新增联系人信息异常" + e.getMessage());
		}
		return commonResp;
	}

	public CommonResp<String> update(Contact contact) {
		CommonResp<String> commonResp = new CommonResp<String>();
		try {
			contactMapper.update(contact);
			commonResp.success();
		} catch (Exception e) {
			logger.error("更新联系人信息异常", e);
			commonResp.exception("更新联系人信息异常 " + e.getMessage());
		}
		return commonResp;
	}
}
